package item;

import effects.Effect;
import factory.EffectFactory;
import loadsave.Memento;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.Iterator;

public class InteractiveItem extends Item {

	private Requirements reqs;
    private Effect effect;
    
    public InteractiveItem( String name, Effect effect ) {
        super(name);
        reqs = new Requirements();
        this.effect = effect;
    }
    
    public InteractiveItem( String name, Requirements requirements ) {
        super( name );
        reqs = requirements;
    }
    
    /**
     * accessor for the effect of this item
     * @return
     */
    public Effect getEffect() {
        return effect;
    }
    
    /**
     * make it so that you can't walk on interactive items
     */
    public boolean canBeWalkedOn() {
        return false;
    }
    
    /**
     * have the visitor visit this
     */
    public void accept( visitors.ItemVisitor iv ) {
        iv.visitInteractive(this);
    }

    /**
     * accessor for the requirements of this item
     * @return
     */
    public Requirements getRequirements() {
    	return reqs;
    }

    /**
     * for saving/loading this class
     */
    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "InteractiveItem");
                Iterator<LoadSaveObject> i = InteractiveItem.super.getMemento().getState().getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                	toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
                }
                LoadSaveObject r = reqs.getMemento().getState();
                r.setName("reqs");
                toReturn.add(r.getName(), r);
                LoadSaveObject e = effect.getMemento().getState();
                r.setName("effect");
                toReturn.add(e.getName(), e);
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
                reqs = new Requirements();
                reqs.getMemento().restoreState(lso.get("reqs"));
                effect = EffectFactory.createEffect(lso.get("effect"));
            }
            
        };
    }

	public InteractiveItem() { }
    
	@Override
	public InteractiveItem clone() {
		InteractiveItem toReturn = new InteractiveItem();
		toReturn.getMemento().restoreState(this.getMemento().getState());
		return toReturn;
	}
    
}
